﻿using Microsoft.Extensions.DependencyModel;
using PaymentService.Model;
using PaymentService.Repositorys;
using PaymentService.Services.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using YDT.Payment.Application;

namespace PaymentService.Services
{
    /// <summary>
    /// 商品service实现
    /// </summary>
    [Dependency(ServiceLifetime.Transient)]
    public class PaymentService : CrudAppService<Payment, PaymentDto, Guid>, IPaymentService
    {
        private IPaymentRepository orderRepository;

        /// <summary>
        /// 依赖注入 支付宝或者微信支付的接口
        /// </summary>
        public WebPagePayFacade webPagePayFacade { set; get; }

        public PaymentService(IPaymentRepository repository) : base(repository)
        {
            orderRepository = repository;
        }

        /// <summary>
        /// 依赖注入 支付宝或者微信支付的接口
        /// </summary>
        /// <param name="createOrderPayDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OrderPayDto> CreateOrderPayAsync(CreateOrderPayDto createOrderPayDto)
        {
            //1、创建订单
            WebPagePayResult webPagePayResult = webPagePayFacade.CreatePay(createOrderPayDto.PaymentType,
                                        createOrderPayDto.ProductName,
                                        createOrderPayDto.OrderSn,
                                        createOrderPayDto.OrderTotalPrice.ToString());
            // 2、支付结果转换
            OrderPayDto orderPayDto = new OrderPayDto();
            orderPayDto.PayResult = webPagePayResult.PayResult;

            return orderPayDto;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PaymentDto> UpdateAsync(Guid id, PaymentDto input)
        {
            var data = await orderRepository.GetAsync(id);
            ObjectMapper.Map(input, data);
            Payment pay = await orderRepository.UpdateAsync(data);
            return ObjectMapper.Map<Payment, PaymentDto>(pay);
        }
    }
}
